package org.brazilo.movi.miaamiko;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

public class XMLPullParserHandler {
	List<Indice> indices;
	List<Licao> licoes;
    private Indice indice;
    private Licao licao;
    private String text;
 
    public XMLPullParserHandler() {
    	indices = new ArrayList<Indice>();
    	licoes = new ArrayList<Licao>();
    }
 
    public List<Indice> getIndicess() {
        return indices;
    }
    
    public List<Licao> getLicoes() {
        return licoes;
    }
 
    public List<Indice> parseIndice(InputStream is) {
        XmlPullParserFactory factory = null;
        XmlPullParser parser = null;
        try {
            factory = XmlPullParserFactory.newInstance();
            factory.setNamespaceAware(true);
            parser = factory.newPullParser();
 
            parser.setInput(is, null);
 
            int eventType = parser.getEventType();
            while (eventType != XmlPullParser.END_DOCUMENT) {
                String tagname = parser.getName();
                switch (eventType) {
                case XmlPullParser.START_TAG:
                    if (tagname.equalsIgnoreCase("licao")) {
                        // create a new instance of employee
                    	indice = new Indice();
                    }
                    break;
 
                case XmlPullParser.TEXT:
                    text = parser.getText();
                    break;
 
                case XmlPullParser.END_TAG:
                    if (tagname.equalsIgnoreCase("licao")) {
                        // add employee object to list
                    	indices.add(indice);
                    } else if (tagname.equalsIgnoreCase("titulo")) {
                    	indice.setTitulo(text);
                    } else if (tagname.equalsIgnoreCase("id")) {
                    	indice.setId(Integer.parseInt(text));
                    }
                    break;
 
                default:
                    break;
                }
                eventType = parser.next();
            }
 
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
 
        return indices;
    }
    
    public List<Licao> parseLicao(InputStream is) {
        XmlPullParserFactory factory = null;
        XmlPullParser parser = null;
        try {
            factory = XmlPullParserFactory.newInstance();
            factory.setNamespaceAware(true);
            parser = factory.newPullParser();
 
            parser.setInput(is, null);
 
            int eventType = parser.getEventType();
            while (eventType != XmlPullParser.END_DOCUMENT) {
                String tagname = parser.getName();
                switch (eventType) {
                case XmlPullParser.START_TAG:
                    if (tagname.equalsIgnoreCase("licao")) {
                        // create a new instance of employee
                    	licao = new Licao();
                    }
                    break;
                    
                case XmlPullParser.CDSECT:
                	text = parser.getText();
                	licao.setConteudo(text);
                	break;
 
                case XmlPullParser.TEXT:
                    text = parser.getText();
                    break;   
 
                case XmlPullParser.END_TAG:
                    if (tagname.equalsIgnoreCase("licao")) {
                        // add employee object to list
                    	licoes.add(licao);
                    } else if (tagname.equalsIgnoreCase("titulo")) {
                    	licao.setTitulo(text);
                    }/* else if (tagname.equalsIgnoreCase("conteudo")) {
                    	licao.setConteudo(text);
                    }*/
                    break;
 
                default:
                    break;
                }
                eventType = parser.nextToken();
            }
 
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
 
        return licoes;
    }
}
